<template>
  <el-dialog v-if="showDialog" custom-class="material-dialog" title="返工投入" :visible.sync="showDialog" width="50%" append-to-body="true" @close="closeDialog">
    <div class="titleBar"><i class="el-icon-vertical-bar"></i>当前工单</div>
    <avue-form ref="currentWorkOrderInfo" :option="currentWorkOrderInfoOption" v-model="currentWorkOrderInfo"></avue-form>
    <div class="titleBar"><i class="el-icon-vertical-bar"></i>返工工单</div>
    <avue-form ref="reworkWorkOrderForm" :option="reworkWorkOrderOption" v-model="reworkWorkOrderForm"></avue-form>
    <avue-crud
      ref="materialCrud"
      :option="reworkSnTableOption"
      :page.sync="page"
      :table-loading="loading"
      :data="reworkSnTableData"
      @current-change="currentChange"
      @size-change="sizeChange"
    >
      <template slot="menu" slot-scope="{ index }">
        <el-button icon="el-icon-delete" size="small" type="text" @click="rowDel(index)">删除</el-button>
      </template>
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" icon="el-icon-check" @click="submit">投 入</el-button>
      <el-button size="small" icon="el-icon-circle-close" @click="closeDialog">取 消</el-button>
    </span>
    <common-modal-template ref="commonModalTemplate" :option="tableOption" @getSelectedRows="getSelectedRows"></common-modal-template>
  </el-dialog>
</template>

<script>
import CommonModalTemplate from '@/components/common-modal-template/common-modal-template.vue';
import { reworkInput } from '@/api/product/productSerialNumManage';
import { getParamDetail } from '@/api/system/param';
export default {
  components: {
    CommonModalTemplate
  },
  props: {
    reworkVisible: {
      type: Boolean,
      default: false
    },
    currentWorkOrderInfo: {
      type: Object,
      default: () => {}
    },
    selectionList: {
      type: Array,
      default: true
    }
  },
  watch: {
    reworkVisible: {
      handler(val) {
        this.showDialog = val;
      }
    },
    selectionList(val) {
      this.reworkSnTableData = val.slice(0, 15);
      this.page.total = val.length;
      this.reworkWorkOrderForm.quantity = val.length;
    }
  },
  data() {
    return {
      showDialog: false,
      currentWorkOrderInfoOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '工单号',
            prop: 'orderNo',
            disabled: true
          },
          {
            label: '生产订单',
            prop: 'productionOrderNo',
            disabled: true
          },
          {
            label: '产品编码',
            prop: 'partNo',
            disabled: true
          },
          {
            label: '产品名称',
            prop: 'partName',
            disabled: true
          },
          {
            label: '产品型号',
            prop: 'partModel',
            disabled: true
          },
          {
            label: '产品总数',
            prop: 'planQuantity',
            disabled: true
          }
        ]
      },
      reworkWorkOrderForm: {},
      reworkWorkOrderOption: {
        submitBtn: false,
        emptyBtn: false,
        labelWidth: '125',
        column: [
          {
            label: '返工工单号',
            prop: 'reworkOrderNo',
            type: 'search',
            appendClick: () => {
              this.openWorkOrderModal();
            },
            disabled: true,
            rules: [
              {
                required: true,
                message: '请选择返工工单号',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '返工数量',
            prop: 'quantity',
            disabled: true
          },
          {
            label: '支持工站投入',
            prop: 'isSupportRouteInput',
            labelTip: '若关闭，则该工单工站中不可投入除该处选择之外的产品序列号',
            type: 'switch',
            dicData: [
              {
                label: '否',
                value: false
              },
              {
                label: '是',
                value: true
              }
            ],
            value: true
          }
        ]
      },
      reworkSnTableOption: {
        height: 350,
        rowKey: 'sequenceNumber',
        calcHeight: 30,
        tip: false,
        selection: false,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        searchBtn: false,
        emptyBtn: false,
        refreshBtn: false,
        menu: true,
        columnBtn: false,
        column: [
          {
            label: '产品序列号',
            prop: 'sequenceNumber'
          },
          {
            label: '当前工序',
            prop: 'currRouteName'
          }
        ]
      },
      reworkSnTableData: [],
      loading: false,
      page: {
        total: 0,
        currentPage: 1,
        pageSize: 20
      },
      tableOption: {}
    };
  },
  computed: {},
  methods: {
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.reworkSnTableData = this.selectionList.slice(Number(currentPage - 1) * Number(this.page.pageSize), Number(currentPage) * Number(this.page.pageSize));
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.page.currentPage = 1;
      this.reworkSnTableData = this.selectionList.slice(0, this.page.pageSize);
    },
    rowDel(index) {
      this.selectionList.splice(index, 1);
      this.page.total = this.selectionList.length;
      this.reworkWorkOrderForm.quantity = this.selectionList.length;
      this.reworkSnTableData = this.selectionList.slice(Number(currentPage - 1) * Number(this.page.pageSize), Number(currentPage) * Number(this.page.pageSize));
    },
    closeDialog() {
      this.currentWorkOrderInfo = {};
      this.reworkWorkOrderForm = {};
      this.selectionList = [];
      this.showDialog = false;
      this.$emit('update:reworkVisible', false);
    },
    async openWorkOrderModal() {
      let url = '/api/mes-product/work-order/page?orderTypeList=6,9&routeConfigured=true';
      await getParamDetail({
        paramKey: 'process.isAudit'
      }).then((res) => {
        let paramValue = this._get(res, 'data.data.paramValue', 'false');
        if (paramValue === 'true') {
          url = '/api/mes-product/work-order/page?orderTypeList=6,9&routeConfigured=true&isAudit=true';
        }
      });
      this.$refs.commonModalTemplate.showModal();
      this.tableOption = {
        modalTitle: '选择工单信息（双击进行选择）',
        url: url,
        isPage: true,
        index: false,
        rowSelectionType: 'single',
        searchMenuSpan: 24,
        columnDefs: [
          {
            label: '生产订单',
            prop: 'productionOrderNo'
          },
          {
            label: '生产工单',
            prop: 'orderNo',
            search: true
          },
          {
            label: '工单类型',
            prop: 'orderType',
            dicUrl: '/api/blade-system/dict/dictionary?code=order_type',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '工单数量',
            prop: 'planQuantity'
          },
          {
            label: '产品编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '产品名称',
            prop: 'partName',
            search: true
          },
          {
            label: '产品型号',
            prop: 'partModel',
            search: true
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            search: true,
            props: {
              label: 'dictValue',
              value: 'dictKey'
            },
            dicUrl: '/api/blade-system/dict/dictionary?code=mfg_work_order',
            html: true,
            formatter: this.$statusText.GD(val.status)
          }
        ]
      };
    },
    // 获取选择的模态框行数据
    getSelectedRows(rowData) {
      this.reworkWorkOrderForm.reworkOrderNoId = this._get(rowData, 'id', '');
      this.reworkWorkOrderForm.reworkOrderNo = this._get(rowData, 'orderNo', '');
    },
    submit() {
      if (!this.reworkWorkOrderForm.reworkOrderNo) {
        this.$message.warning('请选择返工工单号！');
        return;
      }
      let list = [];
      this._forEach(this.selectionList, (item) => {
        list.push(item.sequenceNumber);
      });
      let params = {
        originalOrderNoId: this.currentWorkOrderInfo.id,
        reworkOrderNoId: this.reworkWorkOrderForm.reworkOrderNoId,
        isSupportRouteInput: this.reworkWorkOrderForm.isSupportRouteInput,
        reworkInputSns: list
      };
      reworkInput(params).then((res) => {
        this.closeDialog();
        this.$message.success(res.data.msg);
        this.$emit('reload');
      });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .material-dialog {
  margin-top: 5vh !important;
  .el-dialog__body {
    padding: 15px 20px;
    .titleBar {
      margin-bottom: 10px;
      font-weight: bold;
    }
    .infoForm {
      .avue-form__group {
        .el-col {
          &:last-child {
            display: none;
          }
        }
      }
    }
    .avue-crud__menu {
      display: none;
    }
  }
}
</style>
